import React, {PureComponent} from 'react';
import {routerRedux} from 'dva/router';
import {hashHistory} from 'react-router';
import {connect} from 'dva';
import {Link} from 'dva/router';
import {Form, Card, Row, Col, Input,Radio, Select, message, Table, Button, Popconfirm} from 'antd';
import PageHeaderLayout from '../../layouts/PageHeaderLayout';




const FormItem = Form.Item;
const {Option} = Select;
const RadioGroup = Radio.Group;
@connect(state => ({
    adminManage: state.adminManage,
}))
@Form.create()
class PowerListDetails extends PureComponent{
    state={
        userId:'',
        loading:false,
        record:{},
    }

    componentDidMount() {
        if(!this.props.location.state){
            this.props.dispatch(routerRedux.push("/management/queryManagement"))
            return;
        }
        this.setState({
            // userId:this.props.location.state.id,
            record:this.props.location.state.record,
        })
        console.log(this.props.location.state.record)

    };

    handleSubmit = (e) => {
        this.setState({loading: true});
        e.preventDefault();
        const {dispatch, form} = this.props;
        form.validateFields((err, filedValue) => {
            if (err) return;
            const values = {
                ...(this.state.data),
                ...filedValue,
            };
            dispatch({
                type: 'adminManage/updateAdmin',
                payload: values,
                callback: (response) => {
                    if (response.result == 0) {
                        message.success('提交成功');
                        this.handleEditSuccess();
                        this.setState({loading: false});
                    } else {
                        message.error('提交失败');
                        this.setState({loading: false});
                    }
                }
            });
        });
    };
    handleEditSuccess = () => {
        const {dispatch} = this.props;
        dispatch(routerRedux.push("/management/queryManagement"));
    };
    handleCancel = () => {
        const {dispatch} = this.props;
        dispatch(routerRedux.push("/management/queryManagement"));
    };


    render(){

        const {userId,loading,record} = this.state;
        console.log(record);


        const {getFieldDecorator} = this.props.form;
        // const {adminManage:{adminInfo}}=this.props;
        console.log(record);
        const formItemLayout = {
            labelCol: {
                xs: {span: 24},
                sm: {span: 7},
            },
            wrapperCol: {
                xs: {span: 24},
                sm: {span: 12},
                md: {span: 10},
            },
        };

        const submitFormLayout = {
            wrapperCol: {
                xs: {span: 24, offset: 0},
                sm: {span: 10, offset: 7},
            },
        };

        const radioStyle = {
            display: 'block',
            height: '40px',
            lineHeight: '40px',
        };
        return(
            <PageHeaderLayout title="系统权限详情">
                <Card bordered={false}>
                    <Form onSubmit={this.handleSubmit}>
                        <FormItem
                            {...formItemLayout}
                            label=""
                        >
                            {(getFieldDecorator("userId", {initialValue:record.user_id}))(<Input hidden/>)}

                        </FormItem>
                        <FormItem
                            {...formItemLayout}
                            label="用户名"
                        >
                            {/*{(getFieldDecorator("userName", {initialValue: record.user_name}))(<Input />)}*/}
                            {(getFieldDecorator("userName", {initialValue: record.user_name}))(
                                <Input />
                            )}

                        </FormItem>
                        <FormItem
                            {...formItemLayout}
                            label="邮箱"
                        >
                            {(getFieldDecorator("email", {initialValue:record.email}))( <Input /> )}


                        </FormItem>
                        <FormItem
                            {...formItemLayout}
                            label="添加时间"
                        >
                            {(getFieldDecorator("add_time", {initialValue: record.add_time}))(<Input disabled/>)}


                        </FormItem>
                        <FormItem
                            {...formItemLayout}
                            label="管理员角色"
                        >
                            {/*{(getFieldDecorator("roleId", {initialValue: data.list[0].role_id}))(<div>{data.list[0].role_id}</div>)}*/}

                            {(getFieldDecorator("roleId", {initialValue:record.role_id=='平台管理员'?0:record.role_id=='供应商管理员'?1:2}))(<RadioGroup>
                                <Radio style={radioStyle} value={0}>平台管理员</Radio>
                                <Radio style={radioStyle} value={1}>供应商管理员</Radio>
                                <Radio style={radioStyle} value={2}>运营人员</Radio>
                            </RadioGroup>)}
                        </FormItem>
                        <FormItem
                            {...formItemLayout}
                            label="管理员权限"
                        >
                            {(getFieldDecorator("action_list", {initialValue: record.action_list}))(<div>{record.action_list}</div> )}

                        </FormItem>
                        <FormItem {...submitFormLayout} style={{marginTop: 32}}>
                            <Button type="primary" htmlType="submit" loading={loading}
                                    onClick={this.handleSubmit}>提交</Button>
                            <Button style={{marginLeft: 8}} onClick={this.handleCancel}>取消</Button>
                        </FormItem>
                    </Form>
                </Card>
            </PageHeaderLayout>
        )





    }




}
export default PowerListDetails;